{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #X of a Kind in a Deck of Cards"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #math #counting #number-theory"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #数学 #计数 #数论"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: hasGroupsSizeX"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #卡牌分组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给定一副牌，每张牌上都写着一个整数。</p>\n",
    "\n",
    "<p>此时，你需要选定一个数字 <code>X</code>，使我们可以将整副牌按下述规则分成 1 组或更多组：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>每组都有&nbsp;<code>X</code>&nbsp;张牌。</li>\n",
    "\t<li>组内所有的牌上都写着相同的整数。</li>\n",
    "</ul>\n",
    "\n",
    "<p>仅当你可选的 <code>X &gt;= 2</code> 时返回&nbsp;<code>true</code>。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>deck = [1,2,3,4,4,3,2,1]\n",
    "<strong>输出：</strong>true\n",
    "<strong>解释：</strong>可行的分组是 [1,1]，[2,2]，[3,3]，[4,4]\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>deck = [1,1,1,2,2,2,3,3]\n",
    "<strong>输出：</strong>false\n",
    "<strong>解释：</strong>没有满足要求的分组。\n",
    "</pre>\n",
    "\n",
    "<p><br />\n",
    "<strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= deck.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>0 &lt;= deck[i] &lt; 10<sup>4</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [x-of-a-kind-in-a-deck-of-cards](https://leetcode.cn/problems/x-of-a-kind-in-a-deck-of-cards/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [x-of-a-kind-in-a-deck-of-cards](https://leetcode.cn/problems/x-of-a-kind-in-a-deck-of-cards/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,4,4,3,2,1]', '[1,1,1,2,2,2,3,3]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        if len(deck) < 2:\n",
    "            return False\n",
    "        map = {}\n",
    "        for n in deck:\n",
    "            if n in map:\n",
    "                map[n] += 1\n",
    "            else:\n",
    "                map[n] = 1\n",
    "        m = min(map.values())\n",
    "        g = -1\n",
    "        for v in map.values():\n",
    "            if g == -1:\n",
    "                g = v\n",
    "            else:\n",
    "                g = self.gcd(g,v)\n",
    "        return g>=2\n",
    "\n",
    "\n",
    "    def gcd(self, a, b):\n",
    "        while b:\n",
    "            a, b = b, a % b\n",
    "        return a\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        #逐个试X，由2到10000\n",
    "        if len(deck) < 2:\n",
    "            return False\n",
    "        deck.sort()\n",
    "        X = 2\n",
    "        while X <= len(deck):\n",
    "            newlst = [deck[i:X+i] for i in range(0,len(deck),X)]\n",
    "            for lst in newlst:\n",
    "                if len(set(lst)) != 1 or len(lst) != X:\n",
    "                    break\n",
    "            else:\n",
    "                return True\n",
    "            X += 1\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        cnt = Counter(deck)\n",
    "        tmp = cnt[deck[0]]\n",
    "        for k, v in cnt.items():\n",
    "            tmp = math.gcd(v, tmp)\n",
    "            if tmp == 1:\n",
    "                return False\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        if len(deck)<2:\n",
    "            return False\n",
    "        ad={}\n",
    "        while deck:\n",
    "            a=deck.pop()\n",
    "            ad[a]=ad.get(a,0)+1\n",
    "        l=[x for x in ad.values()]        \n",
    "        if 1 in l:\n",
    "            return False\n",
    "\n",
    "        #print(l)        \n",
    "        for i in range(2,min(l)+1):\n",
    "            flag=True\n",
    "            for j in l:\n",
    "                #print(i,j,j%i)\n",
    "                if j%i!=0:\n",
    "                   flag=False\n",
    "                   break\n",
    "            if flag:\n",
    "                return flag\n",
    "        return False\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        count_times = {}\n",
    "        for num in deck:\n",
    "            count_times.setdefault(num, 0)\n",
    "            count_times[num] += 1\n",
    "        min_times = min(count_times.values())\n",
    "        if min_times == 1:\n",
    "            return False\n",
    "        else:\n",
    "            for i in range(min_times, 1, -1):  # 遍历 min_times, min_times-1, ..., 3, 2 => 找最大公因数\n",
    "                if all(value % i == 0 for value in count_times.values()):  # 判断所有次数除以 i 的余数是否为 0，是则存在>=2的最大公约数\n",
    "                    return True\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "                hash_map_deck = {}\n",
    "                deck.sort()\n",
    "                times = 1\n",
    "                for num in deck:\n",
    "                        if num in hash_map_deck.keys():\n",
    "                                times += 1\n",
    "                        else:\n",
    "                                times = 1\n",
    "                        hash_map_deck[num] = times\n",
    "\n",
    "\n",
    "                values =  hash_map_deck.values()\n",
    "                return reduce(gcd, values) >= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        count_times = {}\n",
    "        for num in deck:\n",
    "            count_times.setdefault(num, 0)\n",
    "            count_times[num] += 1\n",
    "        min_times = min(count_times.values())\n",
    "        if min_times == 1:\n",
    "            return False\n",
    "        else:\n",
    "            for i in range(min_times, 1, -1):  # 遍历 min_times, min_times-1, ..., 3, 2 => 找最大公因数\n",
    "                _ = [value % i for value in count_times.values()]  # 所有次数除以 i 的余数\n",
    "                if sum(_) == 0:\n",
    "                    return True\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        vals = collections.Counter(deck).values()\n",
    "        return reduce(gcd, vals) >= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        s={}\n",
    "        for i in deck:\n",
    "            if s.get(i):\n",
    "                s[i]+=1\n",
    "            else:\n",
    "                s[i]=1\n",
    "\n",
    "        return reduce(gcd, s.values()) >= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        new_deck = sorted(Counter(deck).values())\n",
    "        n = new_deck[0]\n",
    "        for num in new_deck:\n",
    "            if num % n:\n",
    "                n = gcd(n,num)\n",
    "        return n > 1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        return reduce(gcd, Counter(deck).values()) >= 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        return reduce(gcd, Counter(deck).values()) > 1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        res = []\n",
    "        s = set(deck)\n",
    "        for i in s:\n",
    "            if deck.count(i) < 2:\n",
    "                return False\n",
    "            else:\n",
    "                res.append(deck.count(i))\n",
    "        res = set(res)\n",
    "        for x in range(2, len(deck) + 1):\n",
    "            temp = 0\n",
    "            for i in res:\n",
    "                if i % x != 0:\n",
    "                    break\n",
    "                else:\n",
    "                    temp += 1\n",
    "            if temp == len(res):\n",
    "                return True\n",
    "        return False\n",
    "                \n",
    "\n",
    "                    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        count = collections.Counter(deck)\n",
    "        N = len(deck)\n",
    "        for X in range(2, N+1):\n",
    "            if N%X==0:\n",
    "                if all(v%X==0 for v in count.values()):\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        # 暴力枚举法,枚举所有可行的x\n",
    "        # 每一组都有x张牌，所以卡牌总数N必须为x的倍数\n",
    "        # 同理，相同数字的牌有count张，也必须为x的倍数\n",
    "        count = collections.Counter(deck) #创建一个计数器对象 count ，并将列表 deck 作为输入，用于统计可迭代对象中元素的出现次数。在这里，它将统计 deck 列表中每个元素的出现次数，并将结果以键值对的形式存储在 count 对象中\n",
    "        N = len(deck)\n",
    "        for X in range(2, N + 1):\n",
    "            if N % X == 0:\n",
    "                # all 关键字会遍历生成器表达式产生的所有布尔值，并返回一个布尔结果。\n",
    "                if all(v % X == 0 for v in count.values()): \n",
    "                    return True\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        vals = collections.Counter(deck).values()\n",
    "        return reduce(gcd, vals) >= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def find(self,a,b):\n",
    "        for i in range(2,min(a,b)+1):\n",
    "            if a % i ==0 and b % i == 0:\n",
    "                return i\n",
    "        return 1\n",
    "\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        tuple1 = {}\n",
    "        if len(deck) < 2:\n",
    "            return False\n",
    "        for i in deck:\n",
    "            if i in tuple1:\n",
    "                tuple1[i] += 1\n",
    "            else:\n",
    "                tuple1[i] = 1\n",
    "        list1 = list(tuple1.values())\n",
    "        maxVal = 2\n",
    "        for i in range(len(list1)-1):\n",
    "            for j in range(i+1,len(list1)):\n",
    "                val = self.find(list1[i],list1[j])\n",
    "                if maxVal > val:\n",
    "                    maxVal = val\n",
    "        if maxVal >=2:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        dic = {}\n",
    "        for num in deck:\n",
    "            try: dic[num] += 1\n",
    "            except: dic[num] = 1\n",
    "        value = sorted(list(set(dic.values())))\n",
    "        print(value)\n",
    "        for p in range(2,value[0] + 1):\n",
    "            for k in value:\n",
    "                if k % p != 0:\n",
    "                    break\n",
    "            else:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, duck: List[int]) -> bool:\n",
    "        a = {}\n",
    "        for x in duck:\n",
    "            if x not in a:\n",
    "                a[x] = 1\n",
    "            else:\n",
    "                a[x] += 1\n",
    "        d = 0\n",
    "        for y in a.values():\n",
    "            d = gcd(y, d)\n",
    "        return d > 1\n",
    "        print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        d = {}\n",
    "        for x in deck:\n",
    "            if x not in d:\n",
    "                d[x] = 1\n",
    "            else:\n",
    "                d[x] += 1\n",
    "        for i in range(2,len(deck)+1):\n",
    "            ret = True\n",
    "            for x in d.keys():\n",
    "                if d[x] % i != 0:\n",
    "                    ret = False\n",
    "                    break;\n",
    "            if ret == True:\n",
    "                return True\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        # 记下每个数字出现的次数\n",
    "        vals = collections.Counter(deck).values()\n",
    "        # 最大公约数\n",
    "        return reduce(gcd, vals) >= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        dic = Counter(deck)\n",
    "        lst = set(dic.values())\n",
    "        ln = len(deck)\n",
    "        for i in range(2, ln + 1):\n",
    "            if ln % i == 0:\n",
    "                if all(n % i == 0 for n in lst):\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        #\n",
    "        d = Counter(deck)\n",
    "        values = set(d.values())\n",
    "        n = len(deck)\n",
    "        mn = n\n",
    "        # for v in values:\n",
    "        #     mn = min(mn, v)\n",
    "        # if mn <= 1:\n",
    "        #     return False\n",
    "\n",
    "        # 求最大公约数，如果>=2，说明可分\n",
    "        # primes = []\n",
    "        # total = len(values)\n",
    "        for i in range(2, mn + 1):\n",
    "            # if n % i != 0:\n",
    "            #     continue\n",
    "\n",
    "            # # 非质数，继续\n",
    "            # flag = False\n",
    "            # for p in primes:\n",
    "            #     if i % p == 0:\n",
    "            #         flag = True\n",
    "            #         break\n",
    "            # if flag:\n",
    "            #     continue\n",
    "            # # 是质数，检查是否是最大公约数\n",
    "            # primes.append(i)\n",
    "            # count = 0\n",
    "            # for value in values:\n",
    "            #     if value % i != 0:\n",
    "            #         break\n",
    "            #     else:\n",
    "            #         count += 1\n",
    "            # if count == total:\n",
    "            #     return True\n",
    "            if all(num % i == 0 for num in values):\n",
    "                return True\n",
    "        #\n",
    "        return False\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "\n",
    "        c = Counter(deck)\n",
    "        cmp = min(c.values())\n",
    "\n",
    "        if cmp == 1:\n",
    "            return False\n",
    "\n",
    "        ldivisor = []\n",
    "        length = cmp + 1\n",
    "\n",
    "        for i in range(2, length):\n",
    "            if cmp % i == 0:\n",
    "                ldivisor.append(i)\n",
    "\n",
    "        print(ldivisor)\n",
    "        for j in ldivisor:\n",
    "            f = 1\n",
    "            for k in c:\n",
    "                if c[k] % j != 0:\n",
    "                    f = 0\n",
    "                    break\n",
    "\n",
    "            if f:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        \n",
    "        \n",
    "        \n",
    "        # my solution ... \n",
    "        \n",
    "        f = {}\n",
    "        for a in deck:\n",
    "            f[a] = f.get(a, 0) + 1\n",
    "        A = list(f.values())\n",
    "        g = A[0]\n",
    "        for v in A[1:]:\n",
    "            g = gcd(g, v)\n",
    "        return g >= 2\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        if len(deck) < 2:\n",
    "            return False\n",
    "        hash_table = dict()\n",
    "        for d in set(deck):\n",
    "            hash_table[d] = deck.count(d)\n",
    "        n = len(deck)\n",
    "        for x in range(2, n+1):\n",
    "            if n % x == 0:\n",
    "                flag = True\n",
    "                for k, v in hash_table.items():\n",
    "                    if v % x != 0:\n",
    "                        flag = False\n",
    "                if flag:\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "import math\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        count_dict = {}\n",
    "        for card_num in deck:\n",
    "            if card_num in count_dict.keys():\n",
    "                count_dict[card_num] +=1\n",
    "            else:\n",
    "                count_dict[card_num] =1\n",
    "        gcd = list(count_dict.values())[0]\n",
    "        for num in count_dict.values():\n",
    "            gcd = math.gcd(gcd,num)\n",
    "        if gcd>1:\n",
    "            return True\n",
    "        else:\n",
    "            return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        vals = collections.Counter(deck).values()\n",
    "        return reduce(gcd, vals) >= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from functools import reduce\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        cnt = Counter(deck)\n",
    "        return reduce(gcd,cnt.values())>=2\n",
    "        # cnt = Counter(deck)\n",
    "        # min_X = min(cnt.values())\n",
    "        # if min_X<2:\n",
    "        #     return False\n",
    "        # if len(set(cnt.values()))==1:\n",
    "        #     return True\n",
    "        # min_gcd = min_X\n",
    "        # for val in cnt.values():\n",
    "        #     temp = gcd(val,min_gcd)\n",
    "        #     if temp<2:\n",
    "        #         return False\n",
    "        #     if temp<min_gcd:\n",
    "        #         min_gcd = temp\n",
    "        # return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        from math import gcd\n",
    "        from functools import reduce\n",
    "        vals = collections.Counter(deck).values()\n",
    "        return reduce(gcd, vals) >=2\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        count = collections.Counter(deck)\n",
    "        n = len(deck)\n",
    "        for x in range(2, n + 1):\n",
    "            if n % x == 0:\n",
    "                if all(v % x == 0 for v in count.values()):\n",
    "                    return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        d = {}\n",
    "        for x in deck:\n",
    "            if x not in d:\n",
    "                d[x] = 1\n",
    "            else:\n",
    "                d[x] += 1\n",
    "        for i in range(2,len(deck)+1):\n",
    "            ret = True\n",
    "            for x in d.keys():\n",
    "                if d[x] % i != 0:\n",
    "                    ret = False\n",
    "                    break;\n",
    "            if ret == False:\n",
    "                continue\n",
    "            else:\n",
    "                return True\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        count = collections.Counter(deck)\n",
    "        N = len(deck)\n",
    "        for X in range(2, N + 1):\n",
    "            if N % X == 0:\n",
    "                if all(v % X == 0 for v in count.values()):\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        g=gcd(*Counter(deck).values())\n",
    "        return g>1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        vals = collections.Counter(deck).values()\n",
    "        return reduce(gcd, vals) >= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        deck.sort()\n",
    "        s = set(deck)\n",
    "        res = []\n",
    "        for i in s:\n",
    "            res.append(deck.count(i))\n",
    "        for x in range(2,len(deck) + 1):\n",
    "            c = 0\n",
    "            for i in res:\n",
    "                if i % x != 0:\n",
    "                    break\n",
    "                else:\n",
    "                    c += 1\n",
    "            if c == len(res):\n",
    "                return True\n",
    "        return False\n",
    "                \n",
    "\n",
    "                    \n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculate_gcd(self,a,b):\n",
    "        if b==0:\n",
    "            return a\n",
    "        else:\n",
    "            return self.calculate_gcd(b,a%b)\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        dic={}\n",
    "        for i in deck:\n",
    "            if i not in dic:\n",
    "                dic[i]=1\n",
    "            else:\n",
    "                dic[i]+=1\n",
    "        x=dic[deck[0]]\n",
    "        for t in dic:\n",
    "            x=self.calculate_gcd(x,dic[t])\n",
    "        if x>=2:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        vals = collections.Counter(deck).values()\n",
    "        return reduce(gcd, vals) >= 2\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        return math.gcd(*Counter(deck).values()) > 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        import math  ###引入math，用于求两个数的最大公因数\n",
    "        a=Counter(deck)  ###数出deck列表中的元素出现次数\n",
    "        b=a.values()     ###去掉重复的出现次数\n",
    "        c=list(set(b))   ###将次数提取到一个列表中\n",
    "        c.sort()        ###将次数从小到大排序\n",
    "        f=[]            ###定义一个空列表\n",
    "        if len(deck)==1:    \n",
    "            return False   ###排除deck列表中只有一个数字的情况\n",
    "        if len(c)==1:\n",
    "            return True    ###当所有元素的出现次数都相同时，这种情况可取\n",
    "        for j in range(len(c)):\n",
    "            if c[j]==1:\n",
    "                return False    ###当某种元素出现次数为1时，那么一定不符合情况，因为X>=2\n",
    "        else:\n",
    "            if len(c)==2:\n",
    "                if math.gcd(int(c[1]),int(c[0]))==1:     \n",
    "                    return False    ###如果两个元素的出现次数的最大公因数=1，那么就无法满足X>=2\n",
    "                else:\n",
    "                    return True\n",
    "            else:\n",
    "                d=math.gcd(int(c[1]),int(c[0]))     ###算出前两个次数的最大公因数\n",
    "                if d==1:\n",
    "                    return False    ###同上，若最大公因数为1，则不符合题意\n",
    "                else:\n",
    "                    for i in range(2,len(c)):   ###将列表中除前两个外的次数全部提取出来与前两个次数的最大公因数相除求余\n",
    "                        e=int(c[i])%d\n",
    "                        f.append(e)     ###将结果放入空列表中\n",
    "                    f.sort()       ###将结果从小到大排序\n",
    "                    if f[-1]==1:    ###当余数为1时，说明列表中有结果无法整除最大公因数，即不满足题意\n",
    "                        return False\n",
    "                    else:\n",
    "                        return True\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck) -> bool:\n",
    "        n = len(deck)\n",
    "        counts = Counter(deck)\n",
    "        unique_set = set(deck)\n",
    "        # deck.sort()\n",
    "        for x in range(2,n+1):\n",
    "            if n%x==0:\n",
    "                for i in unique_set:\n",
    "                    if counts[i]%x!=0:\n",
    "                        break\n",
    "                else:\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        cnt = Counter(deck).values()\n",
    "        return reduce(gcd,cnt) >= 2\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        #\n",
    "        d  = Counter(deck)\n",
    "        values = set(d.values())\n",
    "        n = len(deck)\n",
    "        mn = n\n",
    "        # for v in values:\n",
    "        #     mn = min(mn, v)\n",
    "        # if mn <= 1:\n",
    "        #     return False\n",
    "\n",
    "        # 求最大公约数，如果>=2，说明可分\n",
    "        # primes = []\n",
    "        total = len(values)\n",
    "        for i in range(2, mn + 1):\n",
    "            # if n % i != 0:\n",
    "            #     continue\n",
    "\n",
    "            # # 非质数，继续\n",
    "            # flag = False\n",
    "            # for p in primes:\n",
    "            #     if i % p == 0:\n",
    "            #         flag = True\n",
    "            #         break\n",
    "            # if flag:\n",
    "            #     continue\n",
    "            # # 是质数，检查是否是最大公约数\n",
    "            # primes.append(i)\n",
    "            count = 0\n",
    "            for value in values:\n",
    "                if value % i != 0:\n",
    "                    break\n",
    "                else:\n",
    "                    count += 1\n",
    "            if count == total:\n",
    "                return True\n",
    "        #\n",
    "        return False\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        d = {}\n",
    "        for x in deck:\n",
    "            if x not in d:\n",
    "                d[x] = 1\n",
    "            else:\n",
    "                d[x] += 1\n",
    "        for i in range(2,len(deck)+1):\n",
    "            ret = True\n",
    "            for x in d.keys():\n",
    "                if d[x] % i != 0:\n",
    "                    ret = False\n",
    "                    break;\n",
    "            if ret == True:\n",
    "                return True\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        d = {}\n",
    "        for x in deck:\n",
    "            if x not in d:\n",
    "                d[x] = 1\n",
    "            else:\n",
    "                d[x] += 1\n",
    "        for i in range(2,len(deck)+1):\n",
    "            ret = True\n",
    "            for x in d.keys():\n",
    "                if d[x] % i != 0:\n",
    "                    ret = False\n",
    "                    break;\n",
    "            if ret == True:\n",
    "                return True\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# from collections import Counter\r\n",
    "class Solution:\r\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\r\n",
    "        counter=Counter(deck)\r\n",
    "\r\n",
    "        m=min(counter.values())\r\n",
    "        for i in range(2,m+1):\r\n",
    "\r\n",
    "            for v in counter.values():\r\n",
    "                if v%i:\r\n",
    "                    break\r\n",
    "            else:\r\n",
    "                return True\r\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        cnt=collections.Counter(deck)\n",
    "        val=[]\n",
    "        for v in cnt.values():\n",
    "            val.append(v)\n",
    "        res=reduce(gcd,val)\n",
    "        return res>=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        vals = collections.Counter(deck).values()\n",
    "        return reduce(gcd, vals) >= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        my_dict = {}\n",
    "        for  num in deck:\n",
    "            try:\n",
    "                my_dict[num] += 1\n",
    "            except:\n",
    "                my_dict[num] = 1\n",
    "        my_list = list(my_dict.values())\n",
    "        my_list.sort()\n",
    "        if my_list[0] < 2:\n",
    "            return False\n",
    "        X = my_list[0]\n",
    "        while X >= 2:\n",
    "            flag = 1\n",
    "            for x in my_list:\n",
    "                if x % X != 0:\n",
    "                    flag = 0\n",
    "                    X -= 1\n",
    "            if flag:\n",
    "                return True\n",
    "        return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def calculate_gcd(self,a,b): #计算最大公约数\n",
    "        if b==0:\n",
    "            return a\n",
    "        else:\n",
    "            return self.calculate_gcd(b,a%b)\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        dic={}\n",
    "        for i in deck:\n",
    "            if i not in dic:\n",
    "                dic[i]=1\n",
    "            else:\n",
    "                dic[i]+=1\n",
    "        x=dic[deck[0]]\n",
    "        for t in dic:\n",
    "            x=self.calculate_gcd(x,dic[t])\n",
    "        if x>=2:\n",
    "            return True\n",
    "        else:\n",
    "            return False\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        d = {}\n",
    "        for x in deck:\n",
    "            if x not in d:\n",
    "                d[x] = 1\n",
    "            else:\n",
    "                d[x] += 1\n",
    "        for i in range(2,len(deck)+1):\n",
    "            ret = True\n",
    "            for x in d.keys():\n",
    "                if d[x] % i != 0:\n",
    "                    ret = False\n",
    "                    break;\n",
    "            if ret == True:\n",
    "                return True\n",
    "        return False\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "\n",
    "        c = Counter(deck)\n",
    "        cmp = min(c.values())\n",
    "\n",
    "        if cmp == 1:\n",
    "            return False\n",
    "\n",
    "        ldivisor = []\n",
    "        length = cmp + 1\n",
    "\n",
    "        for i in range(2, length):\n",
    "            if cmp % i == 0:\n",
    "                ldivisor.append(i)\n",
    "\n",
    "        for j in ldivisor:\n",
    "            f = 1\n",
    "            for k in c:\n",
    "                if c[k] % j != 0:\n",
    "                    f = 0\n",
    "                    break\n",
    "\n",
    "            if f:\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        return functools.reduce(math.gcd,collections.Counter(deck).values())>=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        cnt = Counter(deck)\n",
    "        min_X = min(cnt.values())\n",
    "        if min_X<2:\n",
    "            return False\n",
    "        if len(set(cnt.values()))==1:\n",
    "            return True\n",
    "        min_gcd = min_X\n",
    "        for val in cnt.values():\n",
    "            temp = gcd(val,min_gcd)\n",
    "            if temp<2:\n",
    "                return False\n",
    "            if temp<min_gcd:\n",
    "                min_gcd = temp\n",
    "        return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        count={}\n",
    "        for i in deck:\n",
    "            count[i]=count.get(i,0)+1\n",
    "        count=list(count.values())\n",
    "        if len(count)==1:\n",
    "            if count[0]>1:\n",
    "                return True\n",
    "            else:\n",
    "                return False\n",
    "        def get_all(x,y):\n",
    "            res=set()\n",
    "            for i in range(2,min(x,y)+1):\n",
    "                if x%i or y%i:\n",
    "                    continue\n",
    "                else:\n",
    "                    res.add(i)\n",
    "            return res\n",
    "        i=1\n",
    "        n=len(count)\n",
    "        origin_set=get_all(count[0],count[1])\n",
    "        while origin_set and i<n-1:\n",
    "            origin_set=origin_set&get_all(count[i],count[i+1])\n",
    "            i+=1\n",
    "        return origin_set!=set()\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        counter1 = Counter(deck)\n",
    "        res = [i for i in counter1.values()]\n",
    "        # print(res)\n",
    "        if min(res) < 2:\n",
    "            return False\n",
    "        for i in range(2, min(res) + 1):\n",
    "            list1 = []\n",
    "            for data in set(res):\n",
    "                if data % i != 0:\n",
    "                    continue\n",
    "                list1.append(data)\n",
    "            if len(list1) == len(set(res)):\n",
    "                # print(list1)\n",
    "                return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "from math import gcd\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        if not deck:\n",
    "            return False\n",
    "        \n",
    "        # 统计每个数字的出现次数\n",
    "        counter = Counter(deck)\n",
    "        counts = list(counter.values())\n",
    "        \n",
    "        #print(counts)\n",
    "        # 求所有出现次数的最大公约数\n",
    "        common_divisor = counts[0]\n",
    "        for count in counts[1:]:\n",
    "            common_divisor = gcd(common_divisor, count)\n",
    "        \n",
    "        # 判断最大公约数是否大于等于2\n",
    "        return common_divisor >= 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        def huzhi(x,y):\n",
    "            if x == y == 2:\n",
    "                    return 0\n",
    "            elif (x == 2 or y == 2) and(x%y == 0 or y%x == 0):\n",
    "                    return 0\n",
    "            for i in range(2,min(x,y)+1):\n",
    "                if x%i == y%i == 0:\n",
    "                    return 0\n",
    "                \n",
    "            \n",
    "            return 1\n",
    "        if deck == [1]:\n",
    "            return False\n",
    "        res = []\n",
    "        \n",
    "        for i in set(deck):\n",
    "            res.append(deck.count(i))\n",
    "        if len(res) == 1 and res!={1}:\n",
    "            return True\n",
    "        elif any(res[i] == 1 for i in range(len(res))):\n",
    "            return False\n",
    "        else:\n",
    "            \n",
    "            for i in range(len(res)):\n",
    "                for j in range(i+1,len(res)):\n",
    "                    if huzhi(res[i],res[j]):\n",
    "                        return False\n",
    "            return True\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "       count = collections.Counter(deck)\n",
    "       n = len(deck)\n",
    "       for i in range(2,n+1):\n",
    "           if n % i ==0:\n",
    "               if all(v%i==0 for v in count.values()):\n",
    "                   return True\n",
    "       return False\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        vals = collections.Counter(deck).values()\n",
    "        return reduce(gcd, vals) >= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import math\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        cnt = Counter(deck)\n",
    "        res = cnt[deck[0]]\n",
    "        for x in cnt:\n",
    "            res = math.gcd(res, cnt[x])\n",
    "        return res != 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        vals = Counter(deck).values()\n",
    "        return reduce(gcd, vals) >= 2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        if not deck:\n",
    "            return False\n",
    "\n",
    "        # 统计每个数字的出现次数\n",
    "        count = {}\n",
    "        for card in deck:\n",
    "            count[card] = count.get(card, 0) + 1\n",
    "\n",
    "        # 计算次数的最大公约数\n",
    "        values = list(count.values())\n",
    "        greatest_common_divisor = reduce(gcd, values)\n",
    "\n",
    "        return greatest_common_divisor >= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck) -> bool:\n",
    "        n = len(deck)\n",
    "        counts = Counter(deck)\n",
    "        unique_set = set(deck)\n",
    "        # deck.sort()\n",
    "        for x in range(2,n+1):\n",
    "            if n%x==0:\n",
    "                for i in unique_set:\n",
    "                    if counts[i]%x!=0:\n",
    "                        break\n",
    "                else:\n",
    "                    return True\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        dict1 = Counter(deck).values()\n",
    "\n",
    "        return reduce(gcd, dict1) >= 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\n",
    "        hashv = dict()\n",
    "        for i in range(len(deck)):\n",
    "            hashv[deck[i]]=hashv.get(deck[i],0)+1\n",
    "        maxnum = int(sqrt(max(hashv.values())))+1\n",
    "        if int(sqrt(max(hashv.values())))+1<=4:\n",
    "            maxnum = max(hashv.values())+1\n",
    "        else:\n",
    "            maxnum = int(sqrt(max(hashv.values())))+1\n",
    "        for i in range(2,maxnum):\n",
    "            flag = 1\n",
    "            for item in hashv.values():\n",
    "                if item%i!=0:\n",
    "                    flag = 0\n",
    "                    break\n",
    "            if flag ==1 :\n",
    "                return True\n",
    "        return False\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# from collections import Counter\r\n",
    "from math import gcd\r\n",
    "class Solution:\r\n",
    "    def hasGroupsSizeX(self, deck: List[int]) -> bool:\r\n",
    "        counter=Counter(deck)\r\n",
    "        n=len(deck)\r\n",
    "        for v in counter.values():\r\n",
    "            n=gcd(n,v)\r\n",
    "        return n>=2       "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
